
class EarthsdkDebug {
    constructor() {
        let xe2Debug = {};
        try {
            xe2Debug = JSON.parse(localStorage.xe2Debug);
        } catch (error) {
            console.log(`localStorage.xe2Debug未获取到:${localStorage.xe2Debug}`);
            xe2Debug = {};
        }
        this.xe2Debug = xe2Debug;
    }

    set debug(value) { this.xe2Debug.debug = value; this.apply(); }
    get debug() { return this.xe2Debug.debug ?? false; }

    set debugXbsjBase(value) { this.xe2Debug.debugXbsjBase = !!value; this.apply(); }
    get debugXbsjBase() { return this.xe2Debug.debugXbsjBase ?? false; }

    set debugsdkObjs(value) { this.xe2Debug.debugsdkObjs = !!value; this.apply(); }
    get debugsdkObjs() { return this.xe2Debug.debugsdkObjs ?? false; }

    set debugsdkCzm(value) { this.xe2Debug.debugsdkCzm = !!value; this.apply(); }
    get debugsdkCzm() { return this.xe2Debug.debugsdkCzm ?? false; }

    set debugsdkUe(value) { this.xe2Debug.debugsdkUe = !!value; this.apply(); }
    get debugsdkUe() { return this.xe2Debug.debugsdkUe ?? false; }


    apply() { localStorage.xe2Debug = JSON.stringify(this.xe2Debug); }
}

const g_earthsdkDebug = new EarthsdkDebug();
window.g_earthsdkDebug = g_earthsdkDebug;

const defaultUrl = {
    debugXbsjBase: `http://localhost:8210/xbsj-base.js`,
    debugsdkObjs: `http://localhost:8211/earthsdk2s.js`,
    debugsdkCzm: `http://localhost:8212/earthsdk2-cesium.js`,
    debugsdkUe: `http://localhost:8213/earthsdk2-ue.js`,
}

function createBedugScriptTag(url) {
    var hm = document.createElement("script");
    hm.src = url;
    hm.defer = true;
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
}

function setupXe2() {
    const xe2Debug = g_earthsdkDebug;
    if (!xe2Debug.debug) return;
    const { debugXbsjBase, debugsdkObjs, debugsdkCzm, debugsdkUe } = xe2Debug;
    debugXbsjBase && createBedugScriptTag(defaultUrl['debugXbsjBase']);
    debugsdkObjs && createBedugScriptTag(defaultUrl['debugsdkObjs']);
    debugsdkCzm && createBedugScriptTag(defaultUrl['debugsdkCzm']);
    debugsdkUe && createBedugScriptTag(defaultUrl['debugsdkUe']);
}


setupXe2();

class DebugInfoUI {
    constructor() {
        const xe2Debug = g_earthsdkDebug;

        function createDiv(labelName, propName) {
            const input = document.createElement('input');
            input.type = 'checkbox';
            input.checked = xe2Debug[propName];
            input.onchange = ev => {
                xe2Debug[propName] = input.checked;
            };
            const label = document.createElement('label');
            label.innerText = labelName;
            const div = document.createElement('div');
            div.appendChild(input);
            div.appendChild(label);
            return div;
        }

        const legend = document.createElement('legend');
        legend.innerText = '开发调试(Ctrl+F12)';
        const div0 = createDiv('总开关', 'debug');
        const div1 = createDiv('xbsjBase', 'debugXbsjBase');
        const div2 = createDiv('sdkObjs', 'debugsdkObjs');
        const div3 = createDiv('sdkCzm', 'debugsdkCzm');
        const div4 = createDiv('sdkUe', 'debugsdkUe');
        const reloadButton = document.createElement('button');
        reloadButton.innerText = '刷新';
        reloadButton.onclick = () => {
            xe2Debug.apply();
            location.reload();
        }
        reloadButton.style.margin = '4px 4px 0 4px';
        reloadButton.title = `如果发现控制台加载报错，很可能是要调试某项目没有运行起来！`;

        const hideButton = document.createElement('button');
        hideButton.innerText = '隐藏';
        hideButton.onclick = () => { this.show = false; }

        hideButton.style.margin = '4px 4px 0 4px';
        hideButton.title = `隐藏以后，可以通过Ctrl+F12来显示本窗口`;

        const fieldset = document.createElement('fieldset');
        fieldset.appendChild(legend);
        fieldset.appendChild(div0);
        fieldset.appendChild(div1);
        fieldset.appendChild(div2);
        fieldset.appendChild(div3);
        fieldset.appendChild(div4);
        fieldset.appendChild(reloadButton);
        fieldset.appendChild(hideButton);

        const div = document.createElement('div')
        div.style.position = 'absolute';
        div.style.right = '10px';
        div.style.top = '10px';
        div.style.background = 'rgba(200, 200, 0, 0.95)';
        div.style.zIndex = '9999';  //确保调试在最上面
        div.appendChild(fieldset);

        div.style.display = !!xe2Debug.debug ? 'block' : 'none';

        document.body.appendChild(div);

        this._div = div;
    }

    set show(value) {
        this._div.style.display = value ? 'block' : 'none';
    }

    get show() {
        return this._div.style.display === 'block';
    }
}

function showDebugInfo() {
    const debugInfoUI = new DebugInfoUI();
    window.g_earthsdkDebugInfoUI = debugInfoUI;

    // 按键 Ctrl + F12 后，开启或者关闭调试面板
    document.addEventListener('keyup', ev => {
        if (ev.ctrlKey && ev.key === 'F12') {
            debugInfoUI.show = !debugInfoUI.show;
        }
    });
}

document.addEventListener('DOMContentLoaded', showDebugInfo);
